package com.zghhost.stockweb.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.zghhost.stockweb.constans.SInaConstans;
import com.zghhost.stockweb.entity.SinaDadanEntity;
import com.zghhost.stockweb.util.DateUtil;
import com.zghhost.stockweb.util.HttpUtil;
import com.zghhost.stockweb.util.IOUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author guohua.zhang@zhaopin.com.cn
 * @Date 2019/5/18 12:44
 */
@Service
public class SinaDadanService {
    private static Logger log = LoggerFactory.getLogger(SinaDadanService.class);


    public List<SinaDadanEntity> getDayRangeDadan(String sinaSymbol,String sday,String eday)throws Exception{
        return getDayRangeDadan(sinaSymbol,sday,eday,null);
    }

    private List<SinaDadanEntity> getDayRangeDadan(String sinaSymbol,String sday,String eday,List<SinaDadanEntity> data)throws Exception{
        if(data == null){
            data = new ArrayList<>();
        }

        String today = DateUtil.parseDateToStr(new Date(),DateUtil.DATE_FORMAT_YYYY_MM_DD);

        Date sdate = DateUtil.parseStrToDate(sday+" 00:00:00",DateUtil.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
        Date edate = DateUtil.parseStrToDate(eday+" 00:00:00",DateUtil.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);

        Calendar c=Calendar.getInstance();
        c.setTime(sdate);
        c.add(Calendar.SECOND,1);
        int weekday=c.get(Calendar.DAY_OF_WEEK);

        boolean readFromLocal = true;

        //只下载周一-周五
        if(weekday >= 2 && weekday <=6){
            if(edate.getTime() < sdate.getTime()){
                return data;
            }


            File sfile = new File(SInaConstans.DANDAN_LOCAL_PATH+sinaSymbol+"-"+sday+".json");
            String resstr = "[]";
            if(sfile.exists() && !today.equals(sday)){
                resstr = IOUtil.is2str(new FileInputStream(sfile));
            }else {
                readFromLocal = false;
                resstr = HttpUtil.doHttpGet(String.format(SInaConstans.DADAN_URL,sinaSymbol,sday),"gb2312");
                IOUtil.str2file(resstr,sfile);
            }

            if(resstr != null && resstr.startsWith("[") && resstr.endsWith("]")){
                List<SinaDadanEntity> daydata = JSONArray.parseArray(resstr,SinaDadanEntity.class);
                if(daydata != null && daydata.size() > 0){
                    daydata.stream().forEach(e -> {
                        e.setTicktime(sday.concat(" ").concat(e.getTicktime()));
                        e.setDay(sday);
                    });
                }
                data.addAll(daydata);
            }
        }

        sdate = DateUtil.addDate(sdate,Calendar.DATE,1);

        if(sdate.getTime() <= edate.getTime()){
            if(!readFromLocal){
                Thread.sleep(1870);
            }
            getDayRangeDadan(sinaSymbol,DateUtil.parseDateToStr(sdate,DateUtil.DATE_FORMAT_YYYY_MM_DD),eday,data);
        }

        return data;
    }

    public static void main(String[] args)throws Exception{
        Calendar c = Calendar.getInstance();
        System.out.println(c.get(Calendar.DAY_OF_WEEK));
//        SinaDadanService sinaDadan = new SinaDadanService();
//        List<SinaDadanEntity> data = sinaDadan.getDayRangeDadan("sh601908","2019-05-14","2019-05-16");
//        System.out.println(data);
    }
}
